package com.jucai.pppeiyouclient.view.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.adapter.MySkillAdapter;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.base.TokenParam;
import com.jucai.pppeiyouclient.bean.SkillBean;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.SharedPreferencesUtil;
import com.jucai.pppeiyouclient.utils.ToastUtils;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshForRecyclerView;
import com.jucai.pppeiyouclient.weidget.pulltorefresh.PullToRefreshLayout;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MySkillsActivity extends BaseActivity implements PullToRefreshLayout.OnRefreshListener {
    private static final String TAG = "MySkillsActivity : ";
    private PullToRefreshLayout refreshLayout;
    private PullToRefreshForRecyclerView skillsRv;
    private List<SkillBean> skillList = new ArrayList<>();
    private MySkillAdapter skillAdapter;
    private View skillFooter;
    private Handler mHandler = new Handler();
    private int page = 1;

    private TextView skill_detail;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.common_activity_with_ptr);
        ((TextView) findViewById(R.id.common_title_text)).setText("我的技能");
        loadNetData();
        skillAdapter = new MySkillAdapter(MySkillsActivity.this, skillList);
        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refresh_view);
        skillsRv = (PullToRefreshForRecyclerView) findViewById(R.id.content_view);
        skillsRv.setLayoutManager(new LinearLayoutManager(MySkillsActivity.this, LinearLayoutManager.VERTICAL, false));
        skillsRv.setItemAnimator(new DefaultItemAnimator());
        skillsRv.setAdapter(skillAdapter);

        skillFooter = LayoutInflater.from(MySkillsActivity.this).inflate(R.layout.footer_skill, skillsRv, false);
        skill_detail = (TextView) skillFooter.findViewById(R.id.skill_detail);
        skillAdapter.setBottom(skillFooter);

        skill_detail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MySkillsActivity.this, SkillAuthenticateActivity.class));
            }
        });

        skillAdapter.setOnItemClickListener(new CommonRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                SkillBean skill = skillList.get(position);
                switch (skill.getSkill_cert_state()) {
                    case -1:
                        break;
                    case 0:
                        LogUtils.d(skill.getSkill_id() + "|" + skill.getSkill_name());
                        Intent intent = new Intent(MySkillsActivity.this, AuthenticateActivity.class);
                        intent.putExtra("name", skill.getSkill_name());
                        intent.putExtra("id", skill.getSkill_cert_id());
                        startActivity(intent);
                        break;
                    case 1:

                        break;
                    case 2:

                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onItemLongClickListener(View view, final int position) {
                new AlertDialog.Builder(MySkillsActivity.this).setTitle("确认您的选择").setMessage("删除这个技能").setPositiveButton("取消", null).setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SkillBean skill = skillList.get(position);
                        TokenParam tokenParam = new TokenParam(MySkillsActivity.this, "Accompany_Skill.deleteAccompanySkill");
                        tokenParam.put("skill_cert_id", skill.getSkill_cert_id());
                        CommonRequestNetData.getJsonStringData(MySkillsActivity.this, tokenParam.getParam(), true, new CommonRequestNetData.JsonStringCallBack() {
                            @Override
                            public void parseJsonString(String str) {
                                ToastUtils.showToast(str);
                                loadNetData();
                            }
                        });
                    }
                }).show();
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void loadNetData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Skill.getAccompanySkillList");
        param.put("accompany_id", SharedPreferencesUtil.getStringPreference(MySkillsActivity.this, "PREFERENCE", "accompany_id", ""));
        param.put("page", "1");

        CommonRequestNetData.getJsonArrayData(MySkillsActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
            @Override
            public void parseJsonArray(JSONArray jsonArray) {
                LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
                skillList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SkillBean>>() {
                }.getType());
                LogUtils.e(TAG + "skillList = " + skillList);
                skillAdapter.setData(skillList);
            }
        });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        refreshLayout.setOnRefreshListener(this);
        skillFooter.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(MySkillsActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.skill_footer:
                //startActivity(new Intent(MySkillsActivity.this, AuthenticateDetailActivity.class));
                HashMap<String, String> paramLaw = new HashMap<>();
                paramLaw.put("service", "Public_UserIndex.getSinglePageUrl");
                paramLaw.put("article_type","skill_description");
                paramLaw.put("client","accompany");

                CommonRequestNetData.getJsonObjectData(MySkillsActivity.this, paramLaw, false, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        try {
                            String url = jsonObject.getString("URL");
                            Intent intent = new Intent(MySkillsActivity.this, BrowserActivity.class);
                            intent.putExtra("url", url);
                            startActivity(intent);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
        }
    }

    @Override
    public void onRefresh(final PullToRefreshLayout pullToRefreshLayout) {
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadNetData();
                pullToRefreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }

    @Override
    public void onLoadMore(final PullToRefreshLayout pullToRefreshLayout) {
        ++page;
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                HashMap<String, String> param = new HashMap<>();
//                param.put("service", "Accompany_Skill.getAccompanySkillList");
//                param.put("accompany_id", SharedPreferencesUtil.getStringPreference(MySkillsActivity.this, "PREFERENCE", "accompany_id", ""));
//                param.put("page", page + "");
//
//                CommonRequestNetData.getJsonArrayData(MySkillsActivity.this, param, true, new CommonRequestNetData.JsonArrayCallBack() {
//                    @Override
//                    public void parseJsonArray(JSONArray jsonArray) {
//                        LogUtils.e(TAG + "jsArr = " + jsonArray.toString());
//                        List<SkillBean> list = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<SkillBean>>() {
//                        }.getType());
//                        skillList.addAll(list);
//                        LogUtils.e(TAG + "skillList = " + skillList);
//                        skillAdapter.setData(skillList);
//                    }
//                });
                pullToRefreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            }
        }, 500);
    }
}
